package core.messaging;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.IOException;

import net.sourceforge.peertwitter.core.messaging.TweetMessage;

import org.junit.Before;
import org.junit.Test;

import rice.p2p.commonapi.Id;
import rice.p2p.commonapi.NodeHandle;
import rice.p2p.commonapi.rawserialization.OutputBuffer;


public class TweetMessageTest {

	private NodeHandle sender;
	private NodeHandle anotherSender;
	
	@Before
	public void buildSender() {
	this.sender = new NodeHandle() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void serialize(OutputBuffer arg0) throws IOException {
			}
			
			@Override
			public int proximity() {
				return 0;
			}
			
			@Override
			public boolean isAlive() {
				return true;
			}
			
			@Override
			public Id getId() {
				return rice.pastry.Id.build("0x123213");
			}
			
			@Override
			public boolean checkLiveness() {
				return true;
			}
		};
		
		this.anotherSender = new NodeHandle() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public boolean checkLiveness() {
				return true;
			}

			@Override
			public Id getId() {
				return rice.pastry.Id.build("0x12313123213");
			}

			@Override
			public boolean isAlive() {
				return true;
			}

			@Override
			public int proximity() {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public void serialize(OutputBuffer arg0) throws IOException {
				// TODO Auto-generated method stub
				
			}
			
		};
	}
	
	@Test
	public void twoTweetMessagesAreEqualWhenTheirTextSenderAndTimestampAreEqual() {
		long aTimestamp = System.currentTimeMillis();
		String text = "aText";
		
		TweetMessage aMessage = new TweetMessage(sender, aTimestamp, text);
		TweetMessage anotherMessage =  new TweetMessage(sender, aTimestamp, text);
		assertEquals(aMessage, anotherMessage);
	}
	
	@Test
	public void twoTweetMessageAreNotEqualsWhenTheirTextSenderAreDifferent() {
		long aTimestamp = System.currentTimeMillis();
		String text = "aText";
		
		TweetMessage aMessage = new TweetMessage(sender, aTimestamp, text);
		TweetMessage aMessageWithAnotherText =  new TweetMessage(sender, aTimestamp, text+"another");
		
		assertFalse(aMessage.equals(aMessageWithAnotherText));
	}
	
	@Test
	public void twoTweetMessageAreNotEqualsWhenTheirTimestampAreDifferent() {
		long aTimestamp = System.currentTimeMillis();
		String text = "aText";
		
		TweetMessage aMessage = new TweetMessage(sender, aTimestamp, text);
		TweetMessage aMessageWithAnotherText =  new TweetMessage(sender, aTimestamp+1, text);
		
		assertFalse(aMessage.equals(aMessageWithAnotherText));
	}
	
	@Test
	public void twoTweetMessageAreNotEqualsWhenTheirSenderAreDifferent() {
		long aTimestamp = System.currentTimeMillis();
		String text = "aText";
		
		TweetMessage aMessage = new TweetMessage(sender, aTimestamp, text);
		TweetMessage aMessageWithAnotherText =  new TweetMessage(anotherSender, aTimestamp, text);
		
		assertFalse(aMessage.equals(aMessageWithAnotherText));
	}
	
}
